<template>
   <div class="mainDiv">
      <div class="myWarning"><span class="mintui mintui-field-warning" >&nbsp;</span>{{$root.year}}年高考成绩在浙江考试院公示后，将不能修改，请准确填写信息</div>
      <h3 class="alignCenter">浙江省|综合</h3>
      <mt-field class='myField mgtop10 after firstLine' :state="state.score_total" type="number" v-model="userInfo.score_total" placeholder="请输入总分" label="总  分"></mt-field>
      <mt-field class=' after' :state="state.rankNum" type="number" v-model="userInfo.rankNum" placeholder="请输入位次号" label="位次号"></mt-field>
      <mt-checklist class="myRadio"  title="自选科目（最多可选三门）" :max="3" v-model="userInfo.subjects"  :options="options"></mt-checklist>
     <div class="padlr10">
       <mt-button size="large" type="primary" @click="saveScore()">提交</mt-button>
     </div>
   </div>
</template>

<script>
  import { Toast } from 'mint-ui';
  let attrs = [{name:'score_total',reg:/^\d{1,3}$/},
    {name:'rankNum',reg:/^\d{1,9}$/}];
    export default {
      name: "score",
      data() {
        return {
           userInfo:{subjects:[]}
          ,options : ["物理","化学","生物","技术","历史","地理","思想政治"]
          ,state:{score_total:'',rankNum:''}
        }
      },
      methods:{
        saveScore:function(){
          let attr = attrs.find(n=>{return this.state[n.name]!='success'})
          if(attr){
            Toast(attr.name+"输入错误!");
            return;
          }
           if(this.userInfo.subjects.length!=3){
             Toast("请选择三个自选科目!");
             return;
           }
          let data = {id:this.userInfo.id,score_total:this.userInfo.score_total,rankNum:this.userInfo.rankNum,select_subject:this.userInfo.subjects.join(",")}
          let vm = this;
          this.$http.post("/auth/saveScore",data).then(resp=>{
            if(resp.code == 0){
              Toast("保存成功!");
              vm.$root.userInfo = resp.data;
              window.isBack = true;
              window.history.go(-1);
            }
          })
        }
      },
      watch:{userInfo:{
          handler(newVal,oldVal){
            attrs.forEach(n=>{
              if(newVal[n.name]==undefined||newVal[n.name]==''){
                this.state[n.name] = "";
              }else{
                this.state[n.name] = n.reg.test(newVal[n.name])?'success':'error';
              }
            })
          },
          deep:true
        }
      },
      created(){
        let vm = this;
        checkLogin(this,function(){
          if(vm.userInfo.select_subject){
            vm.userInfo.subjects = vm.userInfo.select_subject.split(",");
          }
        });
      }
    }
</script>

<style>
  .myWarning{
    padding: 15px;
    background-color:#FFFADE;
    color: #FC5831;
    font-size: 15px;
  }
  .myRadio{
    margin-top:36px;
  }

  .myRadio .mint-cell{
    float: left;
  }
 
  .myField .mint-cell-wrapper{
    border-bottom: 1px solid #EEEEEE;
    padding: 0;
    margin: 0 10px;
    height: 60px;
  }
  .myField .mint-cell-wrapper{
   border-top: 1px solid #EEEEEE;
  }
</style>
